﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using Peter.Common;
using Peter.Sdw.Data;

namespace Peter.Sdw.Logic
{
   /// <summary>
   /// The view model for editing links.
   /// </summary>
   public class LinkModel : ViewModelBase
   {
      private bool m_NewWindow;
      private Link m_SelectedLink;
      private RelayCommand m_CmdLink;
      private readonly ObservableCollection<Link> m_Links;

      /// <summary>
      /// Initializes a new style model.
      /// </summary>
      /// <param name="styleable">The styleable parent.</param>
      public LinkModel (IStyleable styleable)
      {
         this.Parent = styleable;
         this.m_Links = new ObservableCollection <Link> ();
      }

      /// <summary>
      /// Gets the parent item.
      /// </summary>
      public IStyleable Parent { get; private set; }

      /// <summary>
      /// Gets or Sets the selected link.
      /// </summary>
      public Link SelectedLink
      {
         get { return this.m_SelectedLink; }
         set
         {
            this.m_SelectedLink = value;
            this.RaisePropertyChanged ("SelectedLink");
         }
      }

      /// <summary>
      /// Gets or Sets if this will open in a new window.
      /// </summary>
      public bool NewWindow
      {
         get { return this.m_NewWindow; }
         set
         {
            this.m_NewWindow = value;
            this.RaisePropertyChanged ("NewWindow");
         }
      }

      /// <summary>
      /// Gets the list of links.
      /// </summary>
      public ObservableCollection<Link> Links { get { return this.m_Links; } }

      /// <summary>
      /// Gets the link action command.
      /// </summary>
      public ICommand LinkCommand
      {
         get { return this.m_CmdLink ?? (this.m_CmdLink = new RelayCommand (this.OnLink, this.CanLink)); }
      }

      /// <summary>
      /// Checks to see if we can add/delete/edit a style.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      /// <returns>True if command can execute, otherwise false.</returns>
      private bool CanLink (object obj)
      {
         var action = obj.ToString ().ToLower ();
         if (action == "add") {
            return true;
         }
         return this.SelectedLink != null;
      }

      /// <summary>
      /// Adds/deletes/edits a style.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      private void OnLink (object obj)
      {
         /*var action = obj.ToString ().ToLower ();
         if (action == "add") {
            var link = new Link {
               Url = this.StartStyle,
               EndIndex = this.Parent.EndIndex,
               StartIndex = this.Parent.StartIndex,
               NewWindow = this.NewWindow
            };
            this.Parent.AddLink (link);
         } else if (action == "delete") {
            this.Parent.RemoveLink (this.SelectedLink);
         } else if (action == "edit") {
            this.SelectedLink.Url = this.StartStyle;
            this.SelectedLink.EndIndex = this.Parent.EndIndex;
            this.SelectedLink.StartIndex = this.Parent.StartIndex;
            this.SelectedLink.NewWindow = this.NewWindow;
            this.Parent.EditLink (this.SelectedLink);
         }*/
      }
   }
}
